import { VuexModule, Module, Action, getModule, Mutation } from 'vuex-module-decorators'
import { LGTReward_CONTRACT, LGTRewardAbi } from '@/contract'
import { ethers } from "ethers";
import { toWei } from '@/utils/format'


import store from '@/store'

export interface LGTRewardState {
}

@Module({ dynamic: true, store, name: 'LGTReward', namespaced: true })
class LGTReward extends VuexModule implements LGTRewardState {

    @Action({ rawError: true })
    public isUserExists(address = store.getters.userAddr): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.isUserExists(address).call()
    }

    @Action({ rawError: true })
    public rankInfo(): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.rankInfo().call()
    }
    @Action({ rawError: true })
    public compenInfo(): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.compenInfo().call()
    }

    @Action({ rawError: true })
    public vInfo(): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.vInfo().call()
    }

    @Action({ rawError: true })
    public userRewardInfo(address = store.getters.userAddr): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.userRewardInfo(address).call()
    }

    @Action({ rawError: true })
    public userVInfo(address = store.getters.userAddr): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.userVInfo(address).call()
    }

    @Action({ rawError: true })
    public getDirectsByPage({ pageNum, pageSize }: { pageNum: number, pageSize: number }): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(LGTRewardAbi, LGTReward_CONTRACT);
        return contract.methods.getDirectsByPage(store.getters.userAddr, pageNum - 1, pageSize).call()
    }

    @Action({ rawError: true })
    public async stake({ referrer, amount }: { referrer: string, amount: string | number }): Promise<any> {
        const ethersProvider = new ethers.providers.Web3Provider(
            window.ethereum,
            "any"
        );
        const contract = new ethers.Contract(
            LGTReward_CONTRACT,
            LGTRewardAbi,
            ethersProvider.getSigner()
        );
        const result = await contract.stake(
            referrer,
            toWei(amount)
        );
        return result.wait()
    }

    @Action({ rawError: true })
    public async getReward(id: number | string): Promise<any> {
        const ethersProvider = new ethers.providers.Web3Provider(
            window.ethereum,
            "any"
        );
        const contract = new ethers.Contract(
            LGTReward_CONTRACT,
            LGTRewardAbi,
            ethersProvider.getSigner()
        );
        const result = await contract.getReward(
            id
        );
        return result.wait()
    }

}

export const LGTRewardModule = getModule(LGTReward)